package com.westa.smallshop.cart;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.data.source.GoodsItemDataSource;
import com.westa.smallshop.data.source.remote.BaseDataSource;

import java.util.List;

import timber.log.Timber;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by cy on 10/11/16.
 */

class CartPresenter implements CartContract.Presenter, BaseDataSource.GetListCallBack<GoodsItem> {

    @NonNull
    private final CartContract.View<GoodsItem> mMessageView;

    @NonNull
    private final GoodsItemDataSource mGoodsItemDataSource;

    CartPresenter(@NonNull CartContract.View<GoodsItem> goodsItemView,
                  @NonNull GoodsItemDataSource goodsItemDataSource) {
        mGoodsItemDataSource = checkNotNull(goodsItemDataSource);
        mMessageView = checkNotNull(goodsItemView);

        mMessageView.setPresenter(this);
    }

    @Override
    public void refreshData() {
        mGoodsItemDataSource.getGoodsInCart(new BaseDataSource.LoadListCallBack<GoodsItem>() {
            @Override
            public void onRefreshDataSuccessful(List<GoodsItem> data) {
                mMessageView.setRefreshing(false);
                mMessageView.refreshData(data);
            }

            @Override
            public void onLoadMoreDataSuccessful(List<GoodsItem> data) {

            }

            @Override
            public void onLoadDataFailed() {
                mMessageView.setRefreshing(false);
            }
        });
    }

    @Override
    public void start() {
    }

    @Override
    public void destroy() {
        mGoodsItemDataSource.cancelLoadRequest();

    }

    @Override
    public void deleteFromCart(int goodsId) {
        mGoodsItemDataSource.deleteFromCart(goodsId, new BaseDataSource.LoadCallBack<JSONObject>() {
            @Override
            public void onLoadDataSuccessful(JSONObject data) {

                if (mMessageView.isActive()) {
                    if (TextUtils.equals(data.getString("result"), "0")) {
                        mMessageView.showDeleteSuccessful();
                    } else {
                        mMessageView.showDeleteFiled();
                    }
                }
            }

            @Override
            public void onLoadDataFailed() {
                mMessageView.showDeleteFiled();
            }
        });
    }

    @Override
    public void orderSubmit(String password) {
        mMessageView.showOrderSubmitDialog();
        mGoodsItemDataSource.verifyPassword(password, new BaseDataSource.LoadCallBack<JSONObject>() {
            @Override
            public void onLoadDataSuccessful(JSONObject data) {

                if (mMessageView.isActive()) {
                    if (TextUtils.equals(data.getString("result"), "0")) {
                        mGoodsItemDataSource.orderSubmit(new BaseDataSource.LoadCallBack<JSONObject>() {
                            @Override
                            public void onLoadDataSuccessful(JSONObject data) {
                                mMessageView.dismissDialog();
                                mMessageView.showOrderSubmitSuccessful();
                            }

                            @Override
                            public void onLoadDataFailed() {
                                mMessageView.dismissDialog();
                                mMessageView.showOrderSubmitFailed();
                            }
                        });
                    } else {
                        mMessageView.showPasswordVerifyFailed();
                        mMessageView.dismissDialogProgress();
                    }
                }
            }

            @Override
            public void onLoadDataFailed() {
                mMessageView.showPasswordVerifyFailed();
            }
        });
    }

    @Override
    public void onGetDataSuccessful(List<GoodsItem> data) {

        if (mMessageView.isActive()) {
            mMessageView.setRefreshing(false);
            mMessageView.refreshData(data);
            mMessageView.setEmptyViewVisibility(false);
        }
    }

    @Override
    public void onGetDataFailed() {
        mGoodsItemDataSource.cancelLoadRequest();
    }
}
